<template>
  <div class="dashboard-container">
    <h2>数据统计</h2>
    
    <div class="charts-container">
      <div class="chart-card">
        <h3>销售额趋势</h3>
        <div v-if="dataStore.loading" class="loading">加载中...</div>
        <div v-else>
          <div class="chart-bar-container">
            <div 
              v-for="(value, index) in dashboardData?.salesChart.data" 
              :key="index"
              class="chart-bar"
              :style="{ 
                height: `${(value / maxSales) * 100}%`,
                bottom: '20px'
              }"
            >
              <span class="bar-value">{{ value }}</span>
            </div>
          </div>
          <div class="chart-labels">
            <span v-for="(label, index) in dashboardData?.salesChart.labels" :key="index">
              {{ label }}
            </span>
          </div>
        </div>
      </div>
      
      <div class="chart-card">
        <h3>用户增长趋势</h3>
        <div v-if="dataStore.loading" class="loading">加载中...</div>
        <div v-else>
          <div class="chart-bar-container">
            <div 
              v-for="(value, index) in dashboardData?.userGrowth.data" 
              :key="index"
              class="chart-bar user-bar"
              :style="{ 
                height: `${(value / maxUsers) * 100}%`,
                bottom: '20px'
              }"
            >
              <span class="bar-value">{{ value }}</span>
            </div>
          </div>
          <div class="chart-labels">
            <span v-for="(label, index) in dashboardData?.userGrowth.labels" :key="index">
              {{ label }}
            </span>
          </div>
        </div>
      </div>
    </div>
    
    <div class="top-products">
      <h3>热销产品</h3>
      <div v-if="dataStore.loading" class="loading">加载中...</div>
      <table v-else>
        <thead>
          <tr>
            <th>ID</th>
            <th>产品名称</th>
            <th>销量</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="product in dashboardData?.topProducts" :key="product.id">
            <td>{{ product.id }}</td>
            <td>{{ product.name }}</td>
            <td>{{ product.sales }}</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script setup>
import { useDataStore } from '@/stores/data'
import { computed } from 'vue'

const dataStore = useDataStore()
const dashboardData = dataStore.dashboardData

// 计算最大销售额用于图表比例
const maxSales = computed(() => {
  if (!dashboardData?.salesChart?.data || dashboardData.salesChart.data.length === 0) {
    return 1
  }
  return Math.max(...dashboardData.salesChart.data)
})

// 计算最大用户数用于图表比例
const maxUsers = computed(() => {
  if (!dashboardData?.userGrowth?.data || dashboardData.userGrowth.data.length === 0) {
    return 1
  }
  return Math.max(...dashboardData.userGrowth.data)
})
</script>

<style scoped>
.dashboard-container {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.charts-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 1.5rem;
}

.chart-card {
  background-color: white;
  padding: 1.5rem;
  border-radius: 8px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.chart-card h3 {
  margin: 0 0 1.5rem 0;
  color: #555;
}

.chart-bar-container {
  display: flex;
  align-items: flex-end;
  gap: 1rem;
  height: 200px;
  padding: 0 1rem;
  position: relative;
}

.chart-bar {
  flex: 1;
  background-color: #3498db;
  border-radius: 4px 4px 0 0;
  position: relative;
  transition: height 0.3s;
}

.user-bar {
  background-color: #2ecc71;
}

.bar-value {
  position: absolute;
  top: -25px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.875rem;
  white-space: nowrap;
}

.chart-labels {
  display: flex;
  justify-content: space-between;
  padding: 0 1rem;
  margin-top: 0.5rem;
}

.chart-labels span {
  flex: 1;
  text-align: center;
  font-size: 0.875rem;
  color: #777;
}

.top-products {
  background-color: white;
  padding: 1.5rem;
  border-radius: 8px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.top-products h3 {
  margin: 0 0 1rem 0;
  color: #555;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  padding: 0.75rem;
  text-align: left;
  border-bottom: 1px solid #eee;
}

th {
  background-color: #f9f9f9;
  color: #555;
}

.loading {
  color: #777;
  padding: 2rem;
  text-align: center;
}
</style>
